package default01;

import java.util.ArrayList;
import java.util.List;

/**
 * @TODO
 * @Author xu
 * 2025/3/15
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LoginTest02 {
    public static void main(String[] args) {
        // 创建用户集合
        List<User> userList = new ArrayList<>();

        // 添加5个用户对象到集合中
        userList.add(new User("user1", "pass1"));
        userList.add(new User("user2", "pass2"));
        userList.add(new User("user3", "pass3"));
        userList.add(new User("user4", "pass4"));
        userList.add(new User("user5", "pass5"));

        // 创建扫描器对象用于输入
        Scanner scanner = new Scanner(System.in);
        int attempts = 3; // 允许输入次数

        while (attempts > 0) {
            System.out.print("请输入用户名: ");
            String username = scanner.nextLine();
            System.out.print("请输入密码: ");
            String password = scanner.nextLine();

            // 验证用户名和密码
            boolean loginSuccess = false;
            for (User user : userList) {
                if (user.matches(username, password)) {
                    loginSuccess = true;
                    break;
                }
            }

            if (loginSuccess) {
                System.out.println("登录成功！");
                break; // 登录成功退出循环
            } else {
                attempts--;
                if (attempts > 0) {
                    System.out.println("用户名或密码错误，请重试。还剩 " + attempts + " 次机会。");
                } else {
                    System.out.println("登录失败，已达到最大尝试次数。");
                }
            }
        }

        // 关闭扫描器
        scanner.close();
    }

}
